define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var popwin = require('@base/dialog').base;
	var util = require('util');

	//加载快捷键配置文件
	var shortCutConfig = require('/config/shortCutConfig');

	//暴露接口
	//1.弹窗
	//2.绑定键盘事件
	var shortCutPopwin = popwin.extend({
		init:function(config){
			config = pubjs.conf(config,{
					class:'P-shortCutMenu',
					'title': LANG('快捷键菜单'),
					'width': 750,
					'height':265,
					'silence': false,
					showFoot:false
				}
			);

			this.Super('init',arguments);
		},
		afterBuild:function(){
			this.Super('afterBuild', arguments);

			// var c = this.getConfig();
			var el = this.getDOM('body');
			var cls = this.getConfig('class');

			el.addClass(cls);
			el.append(this.renderContent());
		},
		renderContent:function(){
			var html = '<table><tbody>';
			var config = shortCutConfig.shortCutSetting;
			for(var k in config){
				html += '<div class="shortCutPair mb5"><a href="'+ config[k].url +'""><span>' + config[k].title + '</span></a><span><strong class="ml15">'+ config[k].key + '</strong></span></div>';
			}
			html += '</tbody></table>'
			return html ;
		},
		onOk:function(){
			this.hide();
		}

	});

	//TODO:创建一个快捷键菜单实例

	var popInstance = null;
	exports.getPopwin = function() {
		if (!popInstance) {
			popInstance = pubjs.core.create('shortCutKeyPop', shortCutPopwin);
		}
		return popInstance;
	}
	exports.showPopwin = function() {
		var mod = exports.getPopwin();
		mod.show();
	}

	exports.hidePopwin = function(){
		exports.getPopwin().hide();
	}


	//对传入的jquery Elem 作快捷菜单绑定
	var bindShortCut = function(){


		var _specialKeys = shortCutConfig.keyCodeMap;


		// 该方法将按钮事件转换成具体的字符串
		 var _keyupEventToString = function(ev) {
			var code = ev.which;
			var keyStr;
			if (_specialKeys[code]) {
				// 特殊键的情况
				keyStr = _specialKeys[code];
			} else {
				// 转换成具体的字符（小写）
				keyStr = String.fromCharCode(code).toLowerCase();
			}

			var combiMap = {
				'shift': ev.shiftKey,
				'ctrl': ev.ctrlKey,
				'alt': ev.altKey,
				// mac下的command键
				'command': ev.metaKey
			};
			var combiStr = '';
			if (combiMap.shift) {combiStr+='shift+'}
			if (combiMap.ctrl) {combiStr+='ctrl+'}
			if (combiMap.alt) {combiStr+='alt+'}
			if (combiMap.command) {combiStr+='command+'}
			keyStr = combiStr + keyStr;

			return keyStr;
		};
			//事件过滤器
		var _keyEventFilters = {
			// 如果在输入框内则要取消快捷键功能
			// 该过滤器是快捷键在input,textarea,select中失效
			editTextCasePass: function(ev) {

				var targetElm = $(ev.target);
				if (targetElm.is("input") || targetElm.is("textarea") || targetElm.is("select")) {
					return true;
				}
			},
			// 该过滤器使只支持ctrl或alt的组合情况
			ctrlOrAltCombiOnly: function(ev) {
				// 如果按键不是已经按下alt或ctrl的情况下，直接pass
				var ctrl = ev.ctrlKey;
				var alt = ev.altKey;
				if (!ctrl && !alt) {
					return true;
				}
				// 如果按键为ctrl或alt的话，也pass掉
				var code = ev.which;
				if (code == 17 || code == 18) {
					return true;
				}
			}
		};

		//考虑到多浏览与多系统支持，使用keydown；不同系统keypress的结果会有很大区别
		var _bindEventName = "keydown";

		//按键名与方法绑定Map
		var _bindingMap = {};

		//绑定到jquery Elem上的事件方法
		var _bindMethod = function(ev){
			for(var k in _keyEventFilters){
				if(_keyEventFilters[k](ev)){
					return true;
				}
			}

			var keyName = _keyupEventToString(ev);

			//触发对应该事件&阻止传递
			var triggerFunc = _bindingMap[keyName];
			return triggerFunc? (triggerFunc(ev) , false): true;

		};

		//绑定事件到元素上
		this.bindJQElm = function(jqElm){
			jqElm.on(_bindEventName,_bindMethod);

		},
		//解除绑定
		this.unbindJQElm = function(jqElm){
			jqElm.off(_bindEventName,_bindMethod);
		}

		//绑定按键和对应事件
		this.bind = function(bindingKey,func){
			bindingKey = bindingKey.toLowerCase();

			_bindingMap[bindingKey] = func;
		}

		//解除按键绑定
		this.unbind = function(bindingKey){
			bindingKey = bindingKey.toLowerCase();
			delete _bindingMap[bindingKey];
		}
		return this;
	}
	//绑定事件
	var shortCutBinder = new bindShortCut();
	// shortCutBinder.bindShortCut
	util.each(shortCutConfig.shortCutSetting,function(item,index){
		var callback = null;

		if(item.name == 'hotkey' ){
			//显示hotkey 窗口
			callback = function(){
				exports.showPopwin();
			};
		} else if(item.url) {
			//绑定封闭方法
			callback = function(){

				if(item.newPage){
					window.open('#' + item.url);
				} else {
					pubjs.controller.navigate(item.url);

				}
			}
		} else {
			return;
		}

		shortCutBinder.bind(item.key,callback);
	});

	shortCutBinder.bindJQElm($(document));
	exports.bindShortCut = bindShortCut;

});